package com.train.controller;

import com.train.dto.CardDTO;
import com.train.dto.EmployeeDTO;
import com.train.pojo.Card;
import com.train.pojo.Department;
import com.train.pojo.Employee;
import com.train.service.CardService;
import com.train.service.DepartmentService;
import com.train.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Collection;
import java.util.List;

@Controller
public class CardController {

    @Autowired
    private CardService cardService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    //查询所有考勤卡，返回列表页面
    @GetMapping("/card")
    public String list(@ModelAttribute("msg")String msg, @ModelAttribute("msg")String msgCode,Model model){
        Collection<CardDTO> cards = cardService.selectAllCardDTO();
//        将结果放在请求中
        model.addAttribute("cards",cards);
        return "card/cardList.html";
    }

    //to考勤卡添加页面
    @GetMapping("/addCard")
    public String toAdd(Model model){
        //查出所有的部门，提供选择
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("departments",departments);
        return "card/addCard.html";
    }
    @GetMapping("/getEmps")
    @ResponseBody
    public List<Employee> employees(int departmentid){
        return employeeService.selectAllEmployeeByDepid(departmentid);
    }

    //员工添加功能，使用post接收
    @PostMapping("/addCard")
    public String add(Model model, Card card,RedirectAttributes rettr){
        //保存员工信息
        cardService.addCard(card);
        rettr.addFlashAttribute("" +
                "msg","考勤卡发放成功");
        rettr.addFlashAttribute("msgCode","200");
        return "redirect:/card";
    }

    //to员工修改页面
    @GetMapping("/card/{cardid}")
    public String toUpdateCard(@PathVariable("cardid") String cardid, Model model){

        CardDTO cardDTO=cardService.selectCardDTOByCardid(cardid);
        model.addAttribute("card",cardDTO);
        //查出所有的部门，提供修改选择
        Collection<EmployeeDTO> employeeDTOS=employeeService.selectAllEmployeeDTO();
        model.addAttribute("emps",employeeDTOS);

        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("departments",departments);

        return "card/updateCard.html";
    }

    @PostMapping("/updateCard")
    public String updateCard(Card card,RedirectAttributes rettr){
        cardService.updateCard(card);
        rettr.addFlashAttribute("" +
                "msg","考勤卡修改成功");
        rettr.addFlashAttribute("msgCode","200");
        //回到员工列表页面
        return "redirect:/card";
    }

    @GetMapping("/delCard/{cardid}")
    public String deleteCard(@PathVariable("cardid")String cardid,RedirectAttributes rettr){
        //根据id删除员工
        cardService.deleteCard(cardid);
        rettr.addFlashAttribute("" +
                "msg","考勤卡删除成功");
        rettr.addFlashAttribute("msgCode","200");
        return "redirect:/card";
    }
}

